//
//  QJHomeVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/30.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ScoreVC.h"

#import "QJCategoryVC.h"

//数据为空
#import "UIScrollView+EmptyDataSet.h"

//第二类分区
#import "YFHomeZeroCell.h"
#import "YFHomeOneCell.h"
#import "YFHomeZeroReusableView.h"
#import "YFHomeOneReusableView.h"
#import "SDCycleScrollView.h"
#import "QJScoreShopDetailVC.h"
@interface ScoreVC () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,SDCycleScrollViewDelegate>{
    
    int _page;
    
}
@property (nonatomic, strong) UICollectionView *homeCollectionView;
@property (nonatomic, strong) SDCycleScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *goodsListArray;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@end

@implementation ScoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"积分专区";
    self.view.backgroundColor = YFColor_Background;
     _page = 1;
    [self setupHomeView];
    [self dataRequestHomeAction];
    self.extendedLayoutIncludesOpaqueBars = YES;
}


#pragma mark -- 界面布局
- (void)setupHomeView{
    
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //滚动方向(默认垂直)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) collectionViewLayout:layout]; // 49 是tabbar的高度
    
    [self.view addSubview:self.homeCollectionView];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.emptyDataSetDelegate = self;
    self.homeCollectionView.emptyDataSetSource = self;
    self.homeCollectionView.backgroundColor = YFColor_Background;
    //在cell的个数不多的时候也可以竖直滑动
    self.homeCollectionView.alwaysBounceVertical = YES;
    
    // 不用Xib,用代码实现
    [self.homeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//    [self.homeCollectionView registerClass:[YFHomeOneReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTwo"];
    
    //cell
    [self.homeCollectionView registerClass:[YFHomeZeroCell class] forCellWithReuseIdentifier:@"cellOne"];
    [self.homeCollectionView registerClass:[YFHomeOneCell class] forCellWithReuseIdentifier:@"cellTwo"];
    
    self.homeCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshNear)];
    self.homeCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshNear)];
}
#pragma mark -- 数据刷新
- (void)headerRefreshNear{
    _page = 1;
    [self dataRequestHomeAction];
}
- (void)footerRefreshNear{
    _page++;
    [self dataRequestHomeAction];
}
#pragma mark -- 数据请求
- (void)dataRequestHomeAction{
    [self.homeCollectionView.mj_footer endRefreshing];
    [self.homeCollectionView.mj_header endRefreshing];
    
    NSNumber *pageNum = [NSNumber numberWithInt:_page];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:pageNum forKey:@"p"];
    
    [WWNetRequest fetchIntegralAreaWithParameters:dict success:^(id response) {
        
        NSLog(@"首页response = %@", response);
        NSDictionary *dic = [response objectForKey:@"data"];
        NSArray *banner = dic[@"banner"];
        NSArray *goodListArr = dic[@"goods_list"];
       
        // 轮播图
        [self.bannerArray removeAllObjects];
        
        for (NSDictionary *dict in banner) {
            HbannerModel *model = [[HbannerModel alloc]initWithDictionary:dict error:nil];
            
            [self.bannerArray addObject:model];
        }
        
        // 商品列表
        NSMutableArray *tempGoodListArr = [NSMutableArray array];
        for (NSDictionary *dict in goodListArr) {
            // 这里先用SearchMdoel ,后期再改名字
            SearchMdoel *model = [[SearchMdoel alloc]initWithDictionary:dict error:nil];
            
            [tempGoodListArr addObject:model];
        }
        
        if (_page == 1) {
            [self.goodsListArray removeAllObjects];
            self.goodsListArray = tempGoodListArr;
        }else {
            for (SearchMdoel *model in tempGoodListArr) {
                [self.goodsListArray addObject:model];
            }
        }
        
        //刷新数据
        [self.homeCollectionView reloadData];
        
    } failure:^(NSDictionary *error) {
        
        
    }];
}

#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodsListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YFHomeOneCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTwo" forIndexPath:indexPath];
    [oneCell cellUpdateWithModel:self.goodsListArray[indexPath.row]];
    return oneCell;
}
#pragma mark --  区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView *headerView = [self.homeCollectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        if(headerView == nil)
        {
            headerView = [[UICollectionReusableView alloc] init];
        }
        headerView.backgroundColor = [UIColor grayColor];
        // 网络加载图片的轮播器  420
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 230) delegate:self placeholderImage:[UIImage imageNamed:@"HBanner"]];
        cycleScrollView.delegate = self;
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"banner_dian"];
        cycleScrollView.pageDotImage = [UIImage imageNamed:@"banner_kong"];
         NSMutableArray *newBannerArr = [NSMutableArray new];
        for (HbannerModel *model in self.bannerArray) {
            NSString *imgUrl = [NSString stringWithFormat:@"%@%@", NET_BASEURL,model.ad_code];
            [newBannerArr addObject:imgUrl];
        }
        cycleScrollView.imageURLStringsGroup = newBannerArr;
        cycleScrollView.delegate = self;
        
        [headerView addSubview:cycleScrollView];
        self.scrollView = cycleScrollView;
        return headerView;
    }

    return nil;
    

}
#pragma mark -- UICollectionViewDelegate
#pragma mark -- 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QJScoreShopDetailVC *VC = [QJScoreShopDetailVC new];
    SearchMdoel *model = self.goodsListArray[indexPath.row];
    VC.goodsType = @"积分商品";
    VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];

    KPushViewControllerAction(VC);
}
#pragma mark - UICollectionViewDelegateFlowLayout -
//定义每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat padding = 1;
    CGFloat w = (UISCREEN_WIDTH - padding*2)/2;
    CGFloat h = w * 550.f/370.f;  // 520.f/370.f;
    return CGSizeMake(w, h);
}

#pragma mark -- item的缩进量
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark -- 设置单元格的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}
#pragma mark -- 设置单元格的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1.0f;
}
#pragma mark -- 区头的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    //    YFHomeZeroReusableView *testView= [[YFHomeZeroReusableView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 0)];
    
    return CGSizeMake(UISCREEN_WIDTH, 230);
}

ARRAY_INIT(bannerArray);
ARRAY_INIT(goodsListArray);
@end

